package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.DAOException;
import dao.DAOfactory;
import dao.LeaveDAO;
import dao.mysql.DAOFactoryImpl;

import model.EmployeeDTO;
import model.LeaveDTO;
import model.LeaveTypeDTO;
import model.LedgerDTO;
import bizlogic.CalculateLeaveDays;
import bizlogic.LeaveManager;
import bizlogic.LedgerManager;

/**
 * Servlet implementation class Servlet111
 */
@WebServlet("/Servlet111")
public class Servlet111 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	int days = 0;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Servlet111() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		LeaveDTO leave = new LeaveDTO();
		
	
		LeaveManager leaveManager = new LeaveManager();
		LedgerManager ledger = new LedgerManager();
		

		HttpSession session = request.getSession();
		EmployeeDTO user = (EmployeeDTO) session.getAttribute("user");
		
		leave.setEmployeeID(user.getEmployeeID());
		leave.setCreateBy(user.getEmployeeID());
			
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setLenient(false);

	//	try {
			
			String str_date = request.getParameter("startdate");
			Date startdate = java.sql.Date.valueOf(str_date);
			
			String end_date = request.getParameter("enddate");
			Date enddate = java.sql.Date.valueOf(end_date);
			
			//java.sql.Date startdate = new java.sql.Date(sdf.parse(
			//		request.getParameter("startdate")).getTime());
			//java.sql.Date enddate = new java.sql.Date(sdf.parse(
			//		request.getParameter("enddate")).getTime());
			
			
			leave.setStartDate((java.sql.Date) startdate);
			leave.setEndDate((java.sql.Date) enddate);
			
			
			
			

		//} catch (Exception e) {
		//	e.printStackTrace();
		//}

		
		
		
		LedgerDTO ledgerReminder = ledger.findLedger(user.getEmployeeID());
		
		
		double remain = 0;
		
		if ((request.getParameter("leavetype")).equals("Annual")) {
			leave.setLeaveTypeID(1);
			remain = (double) ledgerReminder.getRemainAnnualLeave();
		}

		else if ((request.getParameter("leavetype")).equals("Medical")) {
			leave.setLeaveTypeID(2);
			remain = (double) ledgerReminder.getRemainMedicalLeave();
		}

		else if((request.getParameter("leavetype")).equals("Compensation")) {
			leave.setLeaveTypeID(3);
			remain = (ledgerReminder.getRemainOverTimeHr()/8); // for each 4 hr == half day
		}
		
		
		
		System.out.println("====================?===================");
		
		if (user.getRole().equals("Admins")) {
			days = leaveManager.calculate(startdate, enddate);
			if (days < 14) {
				days = leaveManager.calculate2(leave.getStartDate(), leave.getEndDate());
			}

		}

		if (user.getRole().equals("Professional")) {
			days = leaveManager.calculate(leave.getStartDate(), leave.getEndDate());
			if (days < 18) {
				days = leaveManager.calculate2(leave.getStartDate(), leave.getEndDate());
			}
		}

		System.out.println("====================hhhhhhhhh===================");
		session.setAttribute("leave", leave);
		// request.setAttribute(arg0, arg1)
		RequestDispatcher rd = request.getRequestDispatcher("/222.jsp");
		rd.forward(request, response);

	}
		
	
}
